package com.edu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.edu.common.exception.ServiceException;
import com.edu.system.domain.TClass;
import com.edu.system.domain.TClassExt;
import com.edu.system.service.ITClassExtService;
import java.util.HashMap;
import java.util.List;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.edu.system.mapper.TClassroomMapper;
import com.edu.system.domain.TClassroom;
import com.edu.system.service.ITClassroomService;

/**
 * 教室Service业务层处理
 *
 * @author xxxx
 * @date 2023-12-28
 */
@Service
public class TClassroomServiceImpl implements ITClassroomService {
    @Autowired
    private TClassroomMapper tClassroomMapper;
    @Resource
    private ITClassExtService itClassExtService;

    /**
     * 查询教室
     *
     * @param id 教室主键
     * @return 教室
     */
    @Override
    public TClassroom selectTClassroomById(Long id) {
        return tClassroomMapper.selectTClassroomById(id);
    }

    /**
     * 查询教室列表
     *
     * @param tClassroom 教室
     * @return 教室
     */
    @Override
    public List<TClassroom> selectTClassroomList(TClassroom tClassroom) {
        return tClassroomMapper.selectTClassroomList(tClassroom);
    }

    @Override
    public List<TClassroom> selectClassroomList(List<Long> ids) {
        return tClassroomMapper.listByIds(ids);
    }

    @Override
    public Map<String, String> selectLocationIdNameMap() {
        return this.selectTClassroomList(new TClassroom())
            .stream()
            .collect(Collectors.toMap(TClassroom::getCode, TClassroom::getName, (x, y)->x));
    }

    @Override
    public String queryRoomNamesByIds(String ids) {
        if (StrUtil.isBlank(ids)){
            return Strings.EMPTY;
        }
        return this.selectClassroomList(
            Stream.of(ids.split(",")).map(Long::parseLong).collect(Collectors.toList())
        ).stream().map(TClassroom::getName).collect(Collectors.joining(","));
    }

    @Override
    public Map<Long, TClassroom> selectClassroomMap(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return new HashMap<>();
        }
        return this.selectClassroomList(ids)
            .stream()
            .collect(Collectors.toMap(TClassroom::getId, b->b));
    }

    /**
     * 新增教室
     *
     * @param tClassroom 教室
     * @return 结果
     */
    @Override
    public int insertTClassroom(TClassroom tClassroom) {

//        validCode(tClassroom.getCode());
        return tClassroomMapper.insertTClassroom(tClassroom);
    }

    /**
     * 编号（LocationID）校验是否存在
     * 需先更新课表
     * @param classRoomCode
     */
    private void validCode(String classRoomCode) {
        TClassExt ext = new TClassExt();
        ext.setLocationId(classRoomCode);
        List<TClassExt> tClassExts = itClassExtService.selectTClassExtList(ext);
        if (CollectionUtils.isEmpty(tClassExts)){
            throw new ServiceException("编号不存在，检查编号是否正确");
        }
    }

    /**
     * 修改教室
     *
     * @param tClassroom 教室
     * @return 结果
     */
    @Override
    public int updateTClassroom(TClassroom tClassroom) {
//        validCode(tClassroom.getCode());
        return tClassroomMapper.updateTClassroom(tClassroom);
    }

    /**
     * 批量删除教室
     *
     * @param ids 需要删除的教室主键
     * @return 结果
     */
    @Override
    public int deleteTClassroomByIds(Long[] ids) {
        return tClassroomMapper.deleteTClassroomByIds(ids);
    }

    /**
     * 删除教室信息
     *
     * @param id 教室主键
     * @return 结果
     */
    @Override
    public int deleteTClassroomById(Long id) {
        return tClassroomMapper.deleteTClassroomById(id);
    }

    @Override
    public long getCnt() {
        return tClassroomMapper.classroomCnt();
    }
}
